<template>
    <ul class="infinite-list" v-infinite-scroll="load" style="overflow:auto">
            <div style="padding: 10px">
                <el-card class="box-card" shadow="hover">
                    <!--新增区域-->
                    <div style="margin: 10px 0" v-if="user.role===1">
                        <el-button type="primary" @click="add">新增</el-button>
                        <el-popconfirm
                                v-if="user.role === 1"
                                title="确定删除吗？"
                                @confirm="deleteBatch"
                        >
                            <template #reference>
                                <el-button type="danger">批量删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>

                    <!--搜索区域-->
                    <div style="margin: 10px 0">
                        <el-input v-model="pageInfo.query" placeholder="按景点名查询" style="width: 20%" clearable></el-input>
                        <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
                    </div>

                    <!--数据表格-->
                    <el-table :data="tableData" stripe border style="width: 100%"
                              @selection-change="handleSelectionChange">
                        <el-table-column
                                type="selection"
                                width="100">
                        </el-table-column>
                        <!--                <el-table-column prop="id" label="ID" sortable></el-table-column>-->
                        <el-table-column prop="title" label="标题"></el-table-column>
                        <el-table-column prop="address" label="地址"></el-table-column>
                        <el-table-column prop="time" label="开放时间"></el-table-column>
                        <el-table-column prop="phone" label="官方电话"></el-table-column>
                        <el-table-column label="图片">
                            <template #default="scope">
                                <el-image
                                        style="width: 160px;height: 120px"
                                        :src="scope.row.image"
                                        :preview-src-list="[scope.row.image]"
                                ></el-image>
                            </template>
                        </el-table-column>
                        <el-table-column label="公告信息">
                            <template #default="scope">
                                <el-button size="mini" type="warning" plain @click="detalts(scope.row)">公告信息</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="详情">
                            <template #default="scope">
                                <el-button size="mini" type="success" plain @click="detalts1(scope.row)">详情介绍
                                </el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" v-if="user.role===1">
                            <template #default="scope">
                                <el-button size="mini" type="warning" @click="handleEdit(scope.row)">编辑</el-button>
                                <el-popconfirm title="确认删除吗？" @confirm="handleDelete(scope.row.id)">
                                    <template #reference>
                                        <el-button size="mini" type="danger">删除
                                        </el-button>
                                    </template>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>

                    <!--分页-->
                    <div style="margin: 10px 0">
                        <el-pagination
                                @size-change="handleSizeChange"
                                @current-change="handleCurrentChange"
                                :current-page="pageInfo.pageNum"
                                :page-sizes="[5, 10, 20]"
                                :page-size="pageInfo.pageSize"
                                layout="total, sizes, prev, pager, next, jumper"
                                :total="total">
                        </el-pagination>
                    </div>
                </el-card>

                <!--添加按钮的弹出框-->
                <el-dialog title="提示" v-model="dialogVisible" width="30%">
                    <el-form :model="form" label-width="120px">
                        <el-form-item label="标题">
                            <el-input v-model="form.title" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="景区地址">
                            <el-input v-model="form.address" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="开放时间">
                            <el-input v-model="form.time" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="官方电话">
                            <el-input v-model="form.phone" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="景区图片">
                            <el-upload ref="upload" action="http://localhost:9090/files/upload"
                                       :on-success="filesUpload">
                                <el-button type="primary">点击上传</el-button>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="公告信息">
                            <el-input
                                    type="textarea"
                                    :rows="2"
                                    placeholder="请输入公告信息    "
                                    v-model="form.notice">
                            </el-input>
                        </el-form-item>
                        <div id="div1"></div>
                    </el-form>
                    <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
                    </template>
                </el-dialog>


                <el-dialog title="公告" v-model="vis" width="50%">
                    <el-card>
                        <div v-html="detalt.notice" style="min-height: 100px"></div>
                    </el-card>
                </el-dialog>

                <el-dialog title="详情介绍" v-model="vis2" width="50%">
                    <el-card>
                        <div v-html="detalt.details" style="min-height: 100px"></div>
                    </el-card>
                </el-dialog>
            </div>
    </ul>
</template>

<script>


    import request from "../utils/request";
    import E from "wangeditor"

    let editor


    export default {
        name: 'travel',
        components: {},
        data() {
            return {
                pageInfo: {
                    query: '',
                    pageNum: 1,
                    pageSize: 5,
                },
                total: 0,
                tableData: [],
                dialogVisible: false,
                form: {},
                user: {},
                ids: [],
                detalt: {},
                vis: false,
                vis2: false,
            }
        },
        methods: {
            detalts(row) {
                this.detalt = row
                this.vis = true
            },
            detalts1(row) {
                this.detalt = row
                this.vis2 = true
            },
            deleteBatch() {
                if (!this.ids.length) {
                    this.$message.warning("请选择数据！")
                    return
                }
                console.log(this.ids)
                request.post("/travel/deleteBatch", this.ids).then(res => {
                    if (res.code === '0') {
                        this.$message.success("批量删除成功")
                        this.load()
                    } else {
                        this.$message.error(res.msg)
                    }
                })
            },
            handleSelectionChange(val) {
                this.ids = val.map(v => v.id)   // [{id,name}, {id,name}] => [id,id]
            },
            filesUpload(result) {
                this.form.image = result.data;
            },
            async load() {
                const {data: result} = await request.get("/travel/list", {params: this.pageInfo})
                this.tableData = result.rows
                this.total = result.total
            },
            add() {
                this.dialogVisible = true
                this.form = {}
                this.$nextTick(() => {
                    // 关联弹窗里面的div，new一个 editor对象
                    if (!editor) {
                        editor = new E('#div1')
                        // 配置 server 接口地址
                        editor.config.uploadImgServer = 'http://localhost:9090/files/editor/upload'
                        editor.config.uploadFileName = "file"  // 设置上传参数名称
                        editor.create()
                    }
                    editor.txt.html("")
                })
            },
            async save() {
                this.form.details = editor.txt.html()
                if (this.form.id) {//更新
                    const result = await request.put("/travel", this.form)
                    this.dialogVisible = false
                    this.load();
                    if (result.code === '0') {
                        this.$message({
                            type: "success",
                            message: "更新成功"
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: result.msg
                        })
                    }

                } else {//新增
                    const result = await request.post("/travel", this.form)
                    this.dialogVisible = false
                    this.load();
                    if (result.code === '0') {
                        this.$message({
                            type: "success",
                            message: "新增成功"
                        })
                        editor.txt.clear()
                    } else {
                        this.$message({
                            type: 'error',
                            message: result.msg
                        })
                    }
                }
            },
            handleEdit(row) {
                this.form = JSON.parse(JSON.stringify(row))
                this.dialogVisible = true
                this.$nextTick(() => {
                    // 关联弹窗里面的div，new一个 editor对象
                    // 关联弹窗里面的div，new一个 editor对象
                    if (!editor) {
                        editor = new E('#div1')
                        // 配置 server 接口地址
                        editor.config.uploadImgServer = 'http://localhost:9090/files/editor/upload'
                        editor.config.uploadFileName = "file"  // 设置上传参数名称
                        editor.create()
                    }
                    editor.txt.html(row.details)
                })
            },
            handleDelete(id) {
                request.delete("/travel/delete/" + id).then(result => {
                    if (result.code === '0') {
                        this.$message({
                            type: "success",
                            message: "删除成功！"
                        })
                    } else {
                        this.$message({
                            type: "error",
                            message: result.msg
                        })
                    }
                    this.load()
                    console.log(id)
                })


            },
            handleSizeChange(pageSize) {
                this.pageInfo.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {
                this.pageInfo.pageNum = pageNum
                this.load()
            },
            fileUploadSuccess(result) {
                console.log(result)
            },
            async checkUser() {
                const result = await request.get("/user/" + this.user.id)
                if (result === '0') {
                    this.user = result.data
                }
            }
        },
        created() {
            this.load()
            let userStr = sessionStorage.getItem("user") || "{}"
            this.user = JSON.parse(userStr)
        }
    }
</script>
